package com.linkage.educloud.ah.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;

import com.zzhou.common.library.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 和教育自定义表情
 * Created by Mr'Tang on 2018/9/18.
 */

public class ExpressionUtils {
    private static final Integer[] FACE_IMAGE_IDS_JSJ = {R.mipmap.js_jrh,
            R.mipmap.js_kl0, R.mipmap.js_kl1, R.mipmap.js_kl2,
            R.mipmap.js_klnan, R.mipmap.js_klnv, R.mipmap.js_wan,
            R.mipmap.js_xkl0, R.mipmap.js_xkl1, R.mipmap.js_xxls};
    private static final String[] FACE_TEXTS_JSJ = {"[#节日好]", "[#快乐0]",
            "[#快乐1]", "[#快乐2]", "[#快乐男]", "[#快乐女]", "[#爱你]", "[#辛苦0]",
            "[#辛苦1]", "[#谢谢]"};
    private static final Integer[] FACE_IMAGE_IDS = {R.mipmap.weixiao,
            R.mipmap.biti, R.mipmap.bishi, R.mipmap.bishi2,
            R.mipmap.bing, R.mipmap.bukaixin, R.mipmap.chanzui,
            R.mipmap.chi, R.mipmap.chijing, R.mipmap.dasheng,
            R.mipmap.daxiao, R.mipmap.deyi, R.mipmap.fengkou,
            R.mipmap.ganga, R.mipmap.guzhang, R.mipmap.han,
            R.mipmap.huaixiao, R.mipmap.keai, R.mipmap.kelian,
            R.mipmap.koubi, R.mipmap.kouzhao, R.mipmap.ku,
            R.mipmap.kun, R.mipmap.lei, R.mipmap.fennu, R.mipmap.danu,
            R.mipmap.qian, R.mipmap.qinqin, R.mipmap.sajiao,
            R.mipmap.se, R.mipmap.shiai, R.mipmap.shuai, R.mipmap.tu,
            R.mipmap.weiqu, R.mipmap.wuyu, R.mipmap.xindong,
            R.mipmap.xu, R.mipmap.yanjing, R.mipmap.yihuo,
            R.mipmap.yiwen, R.mipmap.youpiezui, R.mipmap.yun,
            R.mipmap.zhayan, R.mipmap.zhaohu, R.mipmap.zhuakuang,
            R.mipmap.ziya, R.mipmap.youpiezui};

    private static final String[] FACE_TEXTS = {"(#微笑)", "(#鼻涕)", "(#鄙视)",
            "(#鄙视2)", "(#病)", "(#不开心)", "(#馋嘴)", "(#吃)", "(#吃惊)", "(#大声)",
            "(#大笑)", "(#得意)", "(#封口)", "(#尴尬)", "(#鼓掌)", "(#汗)", "(#坏笑)",
            "(#可爱)", "(#可怜)", "(#抠鼻)", "(#口罩)", "(#哭)", "(#困)", "(#泪)",
            "(#愤怒)", "(#大怒)", "(#钱)", "(#亲亲)", "(#撒娇)", "(#色)", "(#示爱)",
            "(#衰)", "(#吐)", "(#委屈)", "(#无语)", "(#心动)", "(#嘘)", "(#眼睛)",
            "(#疑惑)", "(#疑问)", "(#右撇嘴)", "(#晕)", "(#眨眼)", "(#招呼)", "(#抓狂)",
            "(#龇牙)", "(#左撇嘴)"};

    public static List<Face> FACES = new ArrayList<Face>();
    public static List<Face> FACES_JSJ = new ArrayList<Face>();
    public static Pattern PATTERN_FACE = Pattern.compile("(\\(#\\w+\\))");
    public static Pattern PATTERN_FACE_JSJ = Pattern.compile("(\\[#\\w+\\])");
    public static Map<String, Integer> FACES_MAP = new HashMap<String, Integer>();
    public static Map<String, Integer> FACES_MAP_JSJ = new HashMap<String, Integer>();

    static {
        for (int i = 0; i < FACE_IMAGE_IDS.length; i++) {
            Face face = new Face(FACE_TEXTS[i], FACE_IMAGE_IDS[i]);
            FACES.add(face);
            FACES_MAP.put(FACE_TEXTS[i], FACE_IMAGE_IDS[i]);
        }
        for (int i = 0; i < FACE_IMAGE_IDS_JSJ.length; i++) {
            Face face = new Face(FACE_TEXTS_JSJ[i], FACE_IMAGE_IDS_JSJ[i]);
            FACES_JSJ.add(face);
            FACES_MAP_JSJ.put(FACE_TEXTS_JSJ[i], FACE_IMAGE_IDS_JSJ[i]);
        }
    }

    public static SpannableString replaceFace(Context context, String text) {
        SpannableString spannableString;

        try {
            if (text == null) {
                text = "";
                spannableString = new SpannableString(text);
                return spannableString;
            }
            spannableString = replaceFaceJsj(context, text);
            Matcher matcher = PATTERN_FACE.matcher(text);
            while (matcher.find()) {
                int start = matcher.start();
                int end = matcher.end();
                Integer integer = FACES_MAP.get(matcher.group());
                if (integer != null) {
                    spannableString.setSpan(getImageSpan(context, integer),
                            start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                }
            }
        } catch (Exception e) {
            text = "";
            spannableString = new SpannableString(text);
            e.printStackTrace();
        }
        return spannableString;
    }

    public static SpannableString replaceFaceSmall(Context context, String text) {
        SpannableString spannableString;
        if (text == null) {
            text = "";
            spannableString = new SpannableString(text);
            return spannableString;
        }
        spannableString = replaceFaceSmallJsj(context, text);
        Matcher matcher = PATTERN_FACE.matcher(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            Integer integer = FACES_MAP.get(matcher.group());
            if (integer != null) {
                spannableString.setSpan(getImageSpanSmall(context, integer),
                        start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return spannableString;
    }

    public static SpannableString replaceFaceJsj(Context context, String text) {
        SpannableString spannableString;

        try {
            if (text == null) {
                text = "";
                spannableString = new SpannableString(text);
                return spannableString;
            }
            spannableString = new SpannableString(text);

            Matcher matcher = PATTERN_FACE_JSJ.matcher(text);
            while (matcher.find()) {
                int start = matcher.start();
                int end = matcher.end();
                Integer integer = FACES_MAP_JSJ.get(matcher.group());
                if (integer != null) {
                    spannableString.setSpan(getImageSpanJsj(context, integer),
                            start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                }
            }
        } catch (Exception e) {
            text = "";
            spannableString = new SpannableString(text);
            e.printStackTrace();
        }

        return spannableString;
    }

    public static SpannableString replaceFaceSmallJsj(Context context,
                                                      String text) {
        SpannableString spannableString;
        if (text == null) {
            text = "";
            spannableString = new SpannableString(text);
            return spannableString;
        }
        spannableString = new SpannableString(text);

        Matcher matcher = PATTERN_FACE_JSJ.matcher(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            Integer integer = FACES_MAP_JSJ.get(matcher.group());
            if (integer != null) {
                spannableString.setSpan(getImageSpanSmall(context, integer),
                        start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        return spannableString;
    }

    public static ImageSpan getImageSpanSmall(Context context, int id) {
        Drawable drawable = context.getResources().getDrawable(id);
        int width = (int) context.getResources().getDimension(
                R.dimen.chat_face_size_small);
        int height = width;

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);

        ImageSpan span = new ImageSpan(context, bitmap,
                ImageSpan.ALIGN_BASELINE);
        return span;
    }

    public static ImageSpan getImageSpan(Context context, int id) {
        Drawable drawable = context.getResources().getDrawable(id);
        int width = (int) context.getResources().getDimension(
                R.dimen.chat_face_size_big) + 15;
        int height = width;

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);

        ImageSpan span = new ImageSpan(context, bitmap,
                ImageSpan.ALIGN_BASELINE);
        return span;
    }

    public static ImageSpan getImageSpanSmallJsj(Context context, int id) {
        Drawable drawable = context.getResources().getDrawable(id);
        int width = (int) context.getResources().getDimension(
                R.dimen.chat_face_size_small);
        int height = width;

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);

        ImageSpan span = new ImageSpan(context, bitmap,
                ImageSpan.ALIGN_BASELINE);
        return span;
    }

    public static ImageSpan getImageSpanJsj(Context context, int id) {
        Drawable drawable = context.getResources().getDrawable(id);

        int width = (int) context.getResources().getDimension(
                R.dimen.chat_face_size_big) + 40;
        int height = width;

        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);

        ImageSpan span = new ImageSpan(context, bitmap,
                ImageSpan.ALIGN_BASELINE);
        return span;
    }

    public static class Face {
        public final String text;
        public final int imageId;

        public Face(String text, int imageId) {
            this.text = text;
            this.imageId = imageId;
        }
    }
}
